<!--
 * @Author: hucc
 * @Date: 2021-10-11 16:12:41
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-11 16:47:10
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue组件传参 子传父</title>
    <script src="../js/vue.js"></script>
    <style>
        .box {
            width: 100px;
            height: 100px;
            border: 1px solid orangered;
        }
    </style>
</head>

<body>
    <div id="app">
        <h2>父组件</h2>
        <div class="box">{{parentData}}</div>
        <!-- 自定义的元素标签中间写字 需要插槽 <slot></slot> -->
        <my-com v-on:child-event="parentHandler">成功按钮</my-com>
    </div>
    <script>
        let component1 = {
            data() {
                return {
                    childData: 'window11'
                }
            },
            methods: {
                toEmitHandler() {
                    //$emit() 可以向上发送事件并且可以携带数据
                    this.$emit('child-event', this.childData)
                }
            },
            template: `
               <div>
                  <h3>子组件</h3>
                  <button>
                    <slot></slot>
                    </button>
                  <button @click="toEmitHandler">发送事件</button>
                </div>
            `
        }
        new Vue({
            el: '#app',
            data: {
                parentData: 'window98'
            },
            methods: {
                //参数data 接收子组件传递过来的数据
                parentHandler(data) {
                    console.log('你说的已经收到了', data);
                    //修改父组件数据，由父组件自己来决定
                    this.parentData = data;
                }
            },
            components: {
                'my-com': component1
            }
        })
    </script>
</body>

</html>